package com.study.sfd.prescription.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.entity.DoctorEntity;
import com.study.common.entity.PatientEntity;
import com.study.common.entity.PrescriptionEntity;
import com.study.common.utils.PageResult;
import com.study.common.utils.Query;
import com.study.common.utils.Result;
import com.study.common.vo.MedicineDetail;
import com.study.common.vo.MedicineWithCover;
import com.study.common.vo.PrescriptionVo;
import com.study.sfd.prescription.config.login.ThreadLocalUtil;
import com.study.sfd.prescription.dao.PrescriptionDao;
import com.study.sfd.prescription.feign.MedicineService;
import com.study.sfd.prescription.feign.PersonService;
import com.study.sfd.prescription.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service("prescriptionService")
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionDao, PrescriptionEntity> implements PrescriptionService
{

    @Override
    public void pay(Integer id)
    {
        PrescriptionEntity prescription = new PrescriptionEntity();
        prescription.setId(id);
        prescription.setStatus(1);
        prescription.setPaymentTime(new Date());
        baseMapper.updateById(prescription);
    }

    @Override
    public PrescriptionVo info(Integer id)
    {
        PrescriptionEntity prescriptionEntity = baseMapper.selectById(id);
        return buildPrescriptionVo(prescriptionEntity);
    }

    private PrescriptionVo buildPrescriptionVo(PrescriptionEntity prescription)
    {
        Result<MedicineDetail> result = medicineService.getMedicineById(prescription.getMedicineId());
        MedicineDetail medicineDetail = result.getData();
        MedicineWithCover medicine = new MedicineWithCover();
        BeanUtils.copyProperties(medicineDetail, medicine);
        medicine.setCover(medicineDetail.getMedicineImgs().get(0));
        PrescriptionVo prescriptionVo = new PrescriptionVo(prescription);
        prescriptionVo.setMedicine(medicine);

        Result<PatientEntity> result1 = personService.getPatient(prescription.getPatientId());
        PatientEntity patient = result1.getData();
        prescriptionVo.setPatient(patient);

        Result<DoctorEntity> result2 = personService.getDoctor(prescription.getDoctorId());
        DoctorEntity doctor = result2.getData();
        prescriptionVo.setDoctor(doctor);

        return prescriptionVo;
    }

    @Override
    public PageResult queryPage(Map<String, Object> params)
    {
        IPage<PrescriptionEntity> page = this.page(
                new Query<PrescriptionEntity>().getPage(params),
                buildQueryWrapper()
        );
        PageResult pageResult = new PageResult(page);

        List<PrescriptionVo> prescriptionVos = page.getRecords().stream()
                .map(this::buildPrescriptionVo)
                .collect(Collectors.toList());
        pageResult.setRecords(prescriptionVos);

        return pageResult;
    }

    private LambdaQueryWrapper<PrescriptionEntity> buildQueryWrapper()
    {
        LambdaQueryWrapper<PrescriptionEntity> queryWrapper = new LambdaQueryWrapper<>();
        Map<String, Object> map = ThreadLocalUtil.get();
        Object patientId = map.get("patientId");
        if (patientId != null) {
            queryWrapper.eq(PrescriptionEntity::getPatientId, patientId);
            return queryWrapper;
        }
        Object doctorId = map.get("doctorId");
        if (doctorId != null) {
            queryWrapper.eq(PrescriptionEntity::getDoctorId, doctorId);
            return queryWrapper;
        }
        Object druggistId = map.get("druggistId");
        Result<List<Integer>> result = personService.getDoctorIdsByDruggistId((Integer) druggistId);
        queryWrapper.in(PrescriptionEntity::getDoctorId, result.getData());
        return queryWrapper;
    }

    @Resource
    MedicineService medicineService;

    @Resource
    private PersonService personService;

    @Override
    public Result<PageResult> listMedicines(Map<String, Object> params)
    {
        Map<String, Object> map = ThreadLocalUtil.get();
        Object doctorId = map.get("doctorId");
        Result<List<Integer>> result = personService.getDruggistIdsByDoctorId((Integer) doctorId);
        params.put("druggistIds", result.getData());
        log.debug(params.toString());
        return medicineService.listMedicines(params);
    }

    @Override
    public int savePrescription(PrescriptionEntity prescription)
    {
        Map<String, Object> map = ThreadLocalUtil.get();
        Object doctorId = map.get("doctorId");
        prescription.setDoctorId((Integer) doctorId);
        prescription.setPrescribeDate(new Date());
        baseMapper.insert(prescription);
        return prescription.getId();
    }
}